package com.oocl.euc.core.requestform.controller;

import com.oocl.euc.common.utils.EntityMapToVOUtil;
import com.oocl.euc.core.requestform.model.RequestForm;
import com.oocl.euc.core.requestform.service.RequestFormService;
import com.oocl.euc.core.requestform.vo.RequestFormVO;
import com.oocl.euc.framework.base.controller.RESTController;
import com.oocl.euc.framework.base.query.BaseConditionQuery;
import com.oocl.euc.framework.base.service.BaseService;
import com.oocl.euc.framework.common.utils.ResponseEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/rest/requestForm")
public class RequestFormController extends RESTController<RequestForm, Long> {

    @Autowired
    RequestFormService requestFormService;

    @Override
    protected BaseService<RequestForm, Long> getBaseService() {
        return requestFormService;
    }

    @RequestMapping(value = "/newSubmission", method = RequestMethod.POST)
    public ResponseEntity createRequestForm(@RequestBody RequestForm requestForm) throws Exception {
        RequestForm resutlt = requestFormService.createRequestForm(requestForm);
        RequestFormVO responResult = EntityMapToVOUtil.map(resutlt, RequestFormVO.class);
        return ResponseEntityUtil.successInRestful(responResult);
    }

    @RequestMapping(value = "/updateRequestForm", method = RequestMethod.POST)
    public ResponseEntity updateRequestForm(@RequestBody RequestForm requestForm) throws MessagingException {

        RequestForm result = requestFormService.updateRequestForm(requestForm);
        RequestFormVO requestFormVO = EntityMapToVOUtil.map(result, RequestFormVO.class);

        setRelateDataToVo(requestForm, requestFormVO);
        return ResponseEntityUtil.successInRestful(requestFormVO);
    }

    @RequestMapping(value = "/findByRefNO/{refNO}", method = RequestMethod.GET)
    public ResponseEntity findRequestFormByRefNO(@PathVariable String refNO) {

        RequestForm requestForm = requestFormService.findRequestFormByRefNO(refNO);
        RequestFormVO requestFormVO = EntityMapToVOUtil.map(requestForm, RequestFormVO.class);

        setRelateDataToVo(requestForm, requestFormVO);
        return ResponseEntityUtil.successInRestful(requestFormVO);
    }

    private void setRelateDataToVo(RequestForm requestForm, RequestFormVO requestFormVO) {
        requestFormVO.setRequestattachments(requestForm.getRequestattachments());
        requestFormVO.setRequestprocedures(requestForm.getRequestprocedures());
        requestFormVO.setRequestpostmortem(requestForm.getRequestpostmortem());
    }

    @RequestMapping(
            value = {"listQuery"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @Override
    @ResponseBody
    public ResponseEntity listWithConditionQuery(@RequestBody BaseConditionQuery<RequestForm> query) {

        List<RequestFormVO> list;
        Page<RequestForm> task = this.getBaseService().findAll(query.buildSpecification(), query.getPageable());
        list = (List<RequestFormVO>) task.stream().map(requestForm -> EntityMapToVOUtil.map(requestForm, RequestFormVO.class)).collect(Collectors.toList());
        Page<RequestFormVO> page = new PageImpl(list, task.getPageable(), task.getTotalElements());
        return ResponseEntityUtil.successInRestful(page);

    }
}
